package com.hxzy.controller.admin;

import com.hxzy.dto.GoodsInfoDTO;
import com.hxzy.dto.PageDTO;
import com.hxzy.entity.TbNewbeeMallGoodsInfo;
import com.hxzy.service.TbNewbeeMallGoodsInfoService;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@Controller
@RequestMapping(value = "/admin")
public class TbNewbeeMallGoodsInfoController {
    @Autowired
    private TbNewbeeMallGoodsInfoService tbNewbeeMallGoodsInfoService;

    @GetMapping(value = "/goods")
    private ModelAndView search(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("admin/infos");

        mv.addObject("path","infos");

        return mv;
    }

    @ResponseBody
    @GetMapping(value = "/infos/data")
    private ResultVO ajaxDate(GoodsInfoDTO pageDTO){
        BootstrapTableVO tableVO=this.tbNewbeeMallGoodsInfoService.search(pageDTO);

        return ResultVO.success(tableVO);
    }

    @ResponseBody
    @GetMapping(value = "/infos/updateStatus")
    private ResultVO updateStatus(TbNewbeeMallGoodsInfo tbNewbeeMallGoodsInfo){
        ResultVO resultVO = this.tbNewbeeMallGoodsInfoService.updateByPrimaryKeySelective(tbNewbeeMallGoodsInfo);

        return resultVO;
    }

    @ResponseBody
    @GetMapping(value = "/infos/add")
    private ModelAndView add(TbNewbeeMallGoodsInfo tbNewbeeMallGoodsInfo){
       ModelAndView mv=new ModelAndView();

        mv.setViewName("admin/goods_info_add");

        mv.addObject("path","infos");

       return mv;
    }

    @ResponseBody
    @PostMapping(value = "/goods/save")
    public ResultVO addOrUpdate(TbNewbeeMallGoodsInfo tbNewbeeMallGoodsInfo, BindingResult bindingResult){
        ResultVO resultVO=null;
        if (bindingResult.getFieldErrorCount()>0){
            String msg = bindingResult.getFieldErrors().get(0).getDefaultMessage();
            resultVO = ResultVO.failed(ResultEnum.PARAMS_FAILED);
            resultVO.setMessage(msg);
            return resultVO;
        }

        return tbNewbeeMallGoodsInfoService.addOrUpdate(tbNewbeeMallGoodsInfo);
    }

    @GetMapping(value = "/goods/edit/{goodsId}")
    public ModelAndView editPage(@PathVariable(value = "goodsId")long goodsId){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("admin/goods_info_edit");
        mv.addObject("path","newbee_mall_goods");
        TbNewbeeMallGoodsInfo tbNewbeeMallGoodsInfo = this.tbNewbeeMallGoodsInfoService.selectByPrimaryKey(goodsId);
        mv.addObject("data",tbNewbeeMallGoodsInfo);
        return mv;
    }
}
